//
//  SJTreeHeaderView.m
//  WSL
//
//  Created by CJ on 2017/11/21.
//  Copyright © 2017年 sanjuetech. All rights reserved.
//

#import "SJTreeHeaderView.h"
#import "SJWordItem.h"
#import "SJExtraItem.h"

@interface SJTreeHeaderView ()

@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UILabel *wordLabel;
@property (strong, nonatomic) UIImageView *coverImageView;
@property (strong, nonatomic) UIView *coverBGView;
@property (strong, nonatomic) UIImageView *mediaImageView;
@property (strong, nonatomic) UIImageView *publishImageView;
@property (strong, nonatomic) UIImageView *subscribeImageView;

@property (strong, nonatomic) UIImageView *titleCoverView;
@property (strong, nonatomic) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *txtTextView;

@end

@implementation SJTreeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self setupUI];
    }
    return self;
}

- (void)setContentSize:(CGSize)contentSize {
    
    _contentSize = contentSize;
    
    [self addSubview:self.contentView];
    self.contentView.sd_layout
    .centerYEqualToView(self)
    .centerXEqualToView(self)
    .heightIs(contentSize.height)
    .widthIs(contentSize.width);
    
    [self.contentView addSubview:self.bgView];
    self.bgView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
    [self.contentView addSubview:self.coverBGView];
    self.coverBGView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
    [self.coverBGView addSubview:self.coverImageView];
    self.coverImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
    [self.contentView addSubview:self.titleCoverView];
    self.titleCoverView.sd_layout
    .leftSpaceToView(self.contentView, 0)
    .rightSpaceToView(self.contentView, 0)
    .bottomSpaceToView(self.contentView, 0)
    .heightIs(SJLayoutFrame(30));
    
    [self.contentView addSubview:self.wordLabel];
    self.wordLabel.sd_layout.spaceToSuperView(UIEdgeInsetsMake(SJLayoutFrame(8), SJLayoutFrame(8), SJLayoutFrame(8), SJLayoutFrame(8)));
    
    [self.contentView addSubview:self.txtTextView];
    self.txtTextView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(SJLayoutFrame(8), SJLayoutFrame(8), SJLayoutFrame(8), SJLayoutFrame(8)));
    
    [self.contentView addSubview:self.mediaImageView];
    self.mediaImageView.sd_layout
    .centerXEqualToView(self.contentView)
    .centerYEqualToView(self.contentView)
    .heightIs(SJLayoutFrame(35))
    .widthIs(SJLayoutFrame(35));
    
    [self.contentView addSubview:self.publishImageView];
    self.publishImageView.sd_layout
    .topSpaceToView(self.contentView, SJLayoutFrame(1))
    .rightSpaceToView(self.contentView, SJLayoutFrame(1))
    .heightIs(SJLayoutFrame(15))
    .widthIs(SJLayoutFrame(16));
    
    [self.contentView addSubview:self.subscribeImageView];
    self.subscribeImageView.sd_layout
    .topSpaceToView(self.contentView, SJLayoutFrame(1))
    .rightSpaceToView(self.contentView, SJLayoutFrame(1))
    .heightIs(SJLayoutFrame(15))
    .widthIs(SJLayoutFrame(16));
    
}


- (void)setItem:(SJWordItem *)item {
    
    _item = item;
    
    SJExtraItem *extraItem = [SJExtraItem modelWithJSON:item.extra];
    
    if ([item.fmt isEqualToString:@"1"]) { // 文字
        
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:SJLayoutFrame(0)];
        paragraphStyle.alignment = NSTextAlignmentCenter;
        
        UIColor *color = [UIColor whiteColor];
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(16)];
        
        NSAttributedString *string = [[NSAttributedString alloc] initWithString:item.content attributes:@{NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: font}];
        
        _wordLabel.attributedText = string;
        
        [_coverImageView setImageURL:nil];
        _mediaImageView.image = nil;
        
        self.wordLabel.hidden = NO;
        self.titleCoverView.hidden = YES;
        
        self.txtTextView.hidden = YES;
        
    } else if ([item.fmt isEqualToString:@"2"]) { // 图片
        
        NSString *urlString = [NSString stringWithFormat:@"%@%@", SJImgURL, item.content];
        
        [_coverImageView setImageWithURL:[NSURL URLWithString:urlString] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionShowNetworkActivity | YYWebImageOptionSetImageWithFadeAnimation];
        
        _mediaImageView.image = nil;
        self.txtTextView.hidden = YES;
        self.wordLabel.hidden = YES;
        
        if ([self.isDisplayTitle isEqualToString:@"1"]) { // 显示title
            self.titleCoverView.hidden = NO;
        } else {
            self.titleCoverView.hidden = YES;
        }
        self.titleLabel.text = extraItem.t?extraItem.t:@"";
        
        
    } else if ([item.fmt isEqualToString:@"3"]) { // 语音
        
        NSString *urlString = [NSString stringWithFormat:@"%@%@", SJImgURL, extraItem.cover];
        [_coverImageView setImageWithURL:[NSURL URLWithString:urlString] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
        
        _mediaImageView.image = [UIImage imageNamed:@"icn_audio"];
        
        self.txtTextView.hidden = YES;
        self.wordLabel.hidden = YES;
        
        if ([self.isDisplayTitle isEqualToString:@"1"]) { // 显示title
            self.titleCoverView.hidden = NO;
        } else {
            self.titleCoverView.hidden = YES;
        }
        self.titleLabel.text = extraItem.t?extraItem.t:@"";
        
        
    } else if ([item.fmt isEqualToString:@"4"]) { // 视频
        
        NSString *urlString = [NSString stringWithFormat:@"%@%@", SJImgURL, extraItem.cover];
        [_coverImageView setImageWithURL:[NSURL URLWithString:urlString] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
        
        _mediaImageView.image = [UIImage imageNamed:@"icn_video"];
        
        self.txtTextView.hidden = YES;
        self.wordLabel.hidden = YES;
        
        if ([self.isDisplayTitle isEqualToString:@"1"]) { // 显示title
            self.titleCoverView.hidden = NO;
        } else {
            self.titleCoverView.hidden = YES;
        }
        self.titleLabel.text = extraItem.t?extraItem.t:@"";
        
        
    } else if ([item.fmt isEqualToString:@"5"]) { // 文章
        
        _wordLabel.text = item.content;
        [_coverImageView setImageURL:nil];
        
        _mediaImageView.image = nil;
        self.wordLabel.hidden = NO;
        self.titleCoverView.hidden = YES;
        self.txtTextView.hidden = YES;
        
    } else if ([item.fmt isEqualToString:@"8"]) { // 空块
        
        _wordLabel.text = @" ";
        [_coverImageView setImageURL:nil];
        _mediaImageView.image = nil;
        self.wordLabel.hidden = YES;
        self.titleCoverView.hidden = YES;
        self.txtTextView.hidden = YES;
        
    } else if ([item.fmt isEqualToString:@"10"]) { // 空间站
        
        _wordLabel.text = @" ";
        [_coverImageView setImageURL:nil];
        _coverImageView.image = [UIImage imageNamed:@"myspace_btn_spacestation"];
        _mediaImageView.image = nil;
        self.wordLabel.hidden = YES;
        self.titleCoverView.hidden = YES;
        item.is_published = @"1";
        item.is_subscribe = @"1";
        self.txtTextView.hidden = YES;
        
    } else if ([item.fmt isEqualToString:@"11"]) { // 文本
        
        [_coverImageView setImageURL:nil];
        _mediaImageView.image = nil;
        self.wordLabel.hidden = YES;
        self.titleCoverView.hidden = YES;
        
        _txtTextView.hidden = NO;
        
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:SJLayoutFrame(6)];
        
        UIColor *color = [UIColor whiteColor];
        UIFont *font = [UIFont systemFontOfSize:SJTxtSize];
        
        NSAttributedString *string = [[NSAttributedString alloc] initWithString:item.content attributes:@{NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: font}];
        
        _txtTextView.attributedText = string;
        
        if ([extraItem.align isEqualToString:@"2"]) {
            _txtTextView.textAlignment = NSTextAlignmentCenter;
        } else {
            _txtTextView.textAlignment = NSTextAlignmentJustified;
        }
    }
    
    if ([item.is_published isEqualToString:@"2"]) { // 1 未发布  2 发布
        self.publishImageView.hidden = NO;
    } else {
        self.publishImageView.hidden = YES;
    }
    
    if ([item.is_subscribe isEqualToString:@"2"]) { // 1 未订阅  2 订阅
        self.subscribeImageView.hidden = NO;
    } else {
        self.subscribeImageView.hidden = YES;
    }
}

#pragma mark - getter

- (UILabel *)txtTextView {
    
    if (!_txtTextView) {
        
        _txtTextView = [UILabel new];
        _txtTextView.backgroundColor = [UIColor clearColor];
        _txtTextView.userInteractionEnabled = NO;
        _txtTextView.numberOfLines = 0;
        _txtTextView.isAttributedContent = YES;
        
    }
    return _txtTextView;
}

- (UIView *)contentView {
    
    if (!_contentView) {
        
        _contentView = [UIView new];
    }
    return _contentView;
}


- (UIImageView *)subscribeImageView{
    
    if (!_subscribeImageView) {
        
        _subscribeImageView = [UIImageView new];
        _subscribeImageView.image = [UIImage imageNamed:@"common_icon_collection"];
        _subscribeImageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    return _subscribeImageView;
}


- (UIImageView *)publishImageView{
    
    if (!_publishImageView) {
        
        _publishImageView = [UIImageView new];
        _publishImageView.image = [UIImage imageNamed:@"icn_publish"];
        _publishImageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    return _publishImageView;
}

- (UIImageView *)mediaImageView{
    
    if (!_mediaImageView) {
        
        _mediaImageView = [UIImageView new];
        _mediaImageView.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    return _mediaImageView;
}

- (UIView *)coverBGView {
    
    if (!_coverBGView) {
        
        _coverBGView = [UIView new];
        _coverBGView.clipsToBounds = YES;
        _coverBGView.sd_cornerRadius = @(SJCornerRadius);
        _coverBGView.userInteractionEnabled = NO;
        
    }
    return _coverBGView;
}

- (UIImageView *)coverImageView{
    
    if (!_coverImageView) {
        
        _coverImageView = [UIImageView new];
        _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImageView.clipsToBounds = YES;
        _coverImageView.userInteractionEnabled = NO;
        
    }
    return _coverImageView;
}

- (UILabel *)wordLabel{
    
    if (!_wordLabel) {    // 词条 16， 文本 14
        
        _wordLabel = [UILabel new];
        _wordLabel.numberOfLines = 2;
        _wordLabel.lineBreakMode = NSLineBreakByClipping;
        _wordLabel.userInteractionEnabled = NO;
        _wordLabel.isAttributedContent = YES;
        
    }
    return _wordLabel;
}

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        
        _titleLabel = [UILabel new];
        _titleLabel.numberOfLines = 1;
//        _titleLabel.lineBreakMode = NSLineBreakByClipping;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(12)];
        
    }
    return _titleLabel;
}

- (UIImageView *)titleCoverView {
    
    if (!_titleCoverView) {
        
        _titleCoverView = [UIImageView new];
        _titleCoverView.contentMode = UIViewContentModeScaleAspectFill;
        _titleCoverView.clipsToBounds = YES;
        _titleCoverView.image = [UIImage imageNamed:@"comomn_titlebg"];
        
        [_titleCoverView addSubview:self.titleLabel];
        self.titleLabel.sd_layout
        .leftSpaceToView(_titleCoverView, SJLayoutFrame(5))
        .rightSpaceToView(_titleCoverView, SJLayoutFrame(5))
        .bottomSpaceToView(_titleCoverView, SJLayoutFrame(5))
        .heightIs(SJLayoutFrame(13));
    }
    return _titleCoverView;
}

- (UIView *)bgView {
    
    if (!_bgView) {
        
        _bgView = [UIView new];
        _bgView.sd_cornerRadius = @(SJCornerRadius);
        _bgView.backgroundColor = SJColorHexString(@"#ffffff");
        _bgView.alpha = .1;
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction)];
        [_bgView addGestureRecognizer:singleTap];
        
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction)];
        doubleTap.numberOfTapsRequired = 2;
        [_bgView addGestureRecognizer:doubleTap];
        
        [singleTap requireGestureRecognizerToFail:doubleTap];
        
    }
    return _bgView;
}

- (void)singleTapAction {
    
//    if (self.singleTapLabel) {
//        self.singleTapLabel(self);
//    }
}

- (void)doubleTapAction {
    
//    if (self.doubleTapLabel) {
//        self.doubleTapLabel(self);
//    }
}

@end

